package it.yuzuojian.com.config;

import it.yuzuojian.com.service.ArticleService;
import it.yuzuojian.com.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class MyTimer {
    @Autowired
    private RedisTemplate myRedisTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ArticleService articleService;
    @Scheduled(cron = "0 */1 * * * ?")
    public void updateArticleViewCount(){
        Set keys = myRedisTemplate.keys("articleid*");
        if(keys.size()!=0){
            for (Object key : keys) {
                Integer id=Integer.parseInt(((String)key).replaceAll("articleid",""));
                Integer viewCount= (Integer) redisUtil.get("articleid"+id);
                articleService.addViewCount(id,viewCount);
            }
        }

    }
}
